package org.example;

import net.e175.klaus.solarpositioning.AzimuthZenithAngle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        //计算太阳高度角的参数，从上到下分别为纬度，经度，海拔高度，大气压力，温度，时区（zoneIdName，zoneId）
        double latitude = 31.79;
        double longitude = 119.46;
        double elevation = 13;
        double pressure = 1016;
        double temperature = 17.6;
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");

        System.out.println("开始计算");

        //计算开始时间
        long start = System.currentTimeMillis();
        //太阳高度角的开始时间节点
        LocalDateTime dateTime = LocalDateTime.of(2025, 7, 8, 6, 0, 0);
        // 获取时间戳（以毫秒为单位）
        long timestamp = dateTime.atZone(zoneId).toInstant().toEpochMilli();
        //表格的地址
        String filePath = "src/main/resources/" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm")) + ".xlsx";

        try (
                Workbook workbook = new HSSFWorkbook()) {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            //循环遍历的计数
            int a = 1;
            // 获取要编辑的工作表
            Sheet sheet = workbook.createSheet("Sheet1");

            Row firstRow = sheet.createRow(0);
            Cell firstRowCell0 = firstRow.createCell(0);
            firstRowCell0.setCellValue("latitude: "+latitude);
            Cell firstCell1 = firstRow.createCell(1);
            firstCell1.setCellValue("longitude: "+longitude);
            Cell firstCell2 = firstRow.createCell(2);
            firstCell2.setCellValue("elevation: "+elevation);
            Cell firstCell3 = firstRow.createCell(3);
            firstCell3.setCellValue("pressure: "+pressure);
            Cell firstCell4 = firstRow.createCell(4);
            firstCell4.setCellValue("temperature: "+temperature);
            Cell firstCell5 = firstRow.createCell(5);
            firstCell5.setCellValue("zoneId: "+zoneId.toString());

            Row secondRow = sheet.createRow(1);
            Cell secondRowCell0 = secondRow.createCell(0);
            secondRowCell0.setCellValue("时间");
            Cell secondRowCell1 = secondRow.createCell(1);
            secondRowCell1.setCellValue("高度角");
            Cell secondRowCell2 = secondRow.createCell(2);
            secondRowCell2.setCellValue("方位角");

            for (int i = 0; i < 50400; i++) {
                a += 1;
                timestamp += 1000;

                // 使用时间戳创建一个Instant对象
                Instant instant = Instant.ofEpochMilli(timestamp);

                // LocalDateTime
                ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId);
                LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();

                //开始计算
                AzimuthZenithAngle result = SpaUtils.getAzimuthAndZenithAngle(localDateTime, zoneId, latitude, longitude, elevation, pressure, temperature);

                //高度角
                double eleAngle = 90 - result.getZenithAngle();
                //方位角
                double azimuth = result.getAzimuth();

                // 创建一个行对象，并在指定单元格中写入数据
                Row row = sheet.createRow(a);
                Cell cell0 = row.createCell(0);
                cell0.setCellValue(localDateTime.toString());
                Cell cell1 = row.createCell(1);
                cell1.setCellValue(eleAngle);
                Cell cell2 = row.createCell(2);
                cell2.setCellValue(azimuth);
//                System.out.println(i);
            }

            // 将修改后的工作簿写入到文件
            FileOutputStream outputStream = new FileOutputStream(filePath);
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
           e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("已完成，excel目录: "+ filePath.toString());
        System.out.println("总耗时为" + (end - start));

    }
}